--- title: "Homework 7 R Exercise" output: pdf_document: default html_document: default --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` We will again explore **t.test** in R, though we will look at two-sample tests rather than one-sample tests. Before doing so, we simulate the difference of sample means, $\bar{x}_1 - \bar{x}_2$, to demonstrate that the variance of the differences is $\sigma_1^2 + \sigma_2^2$; i.e., that the variance is additive. To do so, we generate 1000 samples of size 10 for each of two samples, compute their means and compute the difference of means. We then create side-by-side boxplots of the 1000 Sample 1 means, 1000 Sample 2 means, and 1000 differences in the means, and compute variances for each of $\bar{x}_1$, $\bar{x}_2$ and $\bar{x}_1 - \bar{x}_2$. Explain the different values for the medians you see in each boxplot. Does the boxplot for $\bar{x}_1 - \bar{x}_2$ show more variation than the boxplots for $\bar{x}_1$ and $\bar{x}_2$? Do the sample variances roughly match the population variances for $\bar{x}_1$, $\bar{x}_2$ and $\bar{x}_1 - \bar{x}_2$? ```{r eval=FALSE} #For Sample 1, mean=2 and variance=1 Sample_1=matrix(rnorm(10000,2,1),ncol=10) #For Sample 2, mean=1 and variance=1 Sample_2=matrix(rnorm(10000,1,1),ncol=10) #Compute 1000 Sample 1 means Xbar1=apply(Sample_1,1,mean) #Compute 1000 Sample 2 means Xbar2=apply(Sample_2,1,mean) #Compute 1000 differences Diff=Xbar1-Xbar2 boxplot(cbind(Xbar1,Xbar2,Diff)) #This variance should be around 0.1 var(Xbar1) #This variance should also be around 0.1 var(Xbar2) #This variance should be around 0.2 (0.1+0.1) var(Diff) ``` We can now create simulated data sets to test whether two population means are equal. We actually specify different sample means in our simulation. Did your simulation detect a difference at $\alpha=0.05$ for either procedure? ```{r eval=FALSE} #For Sample 1, n=10, mean=10, standard deviation=1 Sample_1=rnorm(10,10,1) #For Sample 2, n=10, mean=9, standard deviation=2 Sample_2=rnorm(10,9,2) #The default test is Welch's test (unequal variances) t.test(Sample_1,Sample_2,alternative="two.sided") #Use var.equal=TRUE for the pooled t-test t.test(Sample_1,Sample_2,alternative="two.sided",var.equal=TRUE) ```